import { Injectable } from '@nestjs/common';
import { CreateChatDto } from './dto/create-chat.dto';
import { UpdateChatDto } from './dto/update-chat.dto';
import { PrismaService } from 'src/prisma.service';
import { FriendMessage, Group, GroupMap, GroupMessage, Prisma, User, UserMap } from '@prisma/client';
import { TRes } from 'src/common/base';

@Injectable()
export class ChatService {

  constructor(
    private prisma: PrismaService,
  ) {}

  /**
   * 查询用户信息
   * @param userWhereUniqueInput
   * @returns User
   */
  async queryUser(
    userWhereUniqueInput: Prisma.UserWhereUniqueInput,
  ): Promise<User> {
   try {
    const user = await this.prisma.user.findUnique({
      where: userWhereUniqueInput,
    });
    return user
   } catch (error) {}
  }

  /**
   * 查询群组数据
   * @param groupWhereUniqueInput
   * @returns
   */
  async queryGroup(
    groupWhereUniqueInput: Prisma.GroupWhereUniqueInput,
  ): Promise<Group> {
   try {
    const group = await this.prisma.group.findUnique({
      where: groupWhereUniqueInput,
    });
    return group
   } catch (error) {}
  }

  /**
   * 查询群组管理关系
   * @param groupMapWhereInput Prisma.GroupMapWhereInput
   * @returns GroupMap
   */
  async queryGroupMap(
    groupMapWhereInput: Prisma.GroupMapWhereInput,
  ): Promise<GroupMap> {
   try {
    const group = await this.prisma.groupMap.findFirst({
      where: groupMapWhereInput,
    });
    return group
   } catch (error) {}
  }

  /**
   * 查询某个用户和用户之间的对应关系
   * @param userMapWhereInput
   * @returns
   */
  async queryUserMap(
    userMapWhereInput: Prisma.UserMapWhereInput,
  ): Promise<UserMap> {
   try {
    const user = await this.prisma.userMap.findFirst({
      where: userMapWhereInput,
    });
    return user
   } catch (error) {}
  }

   /**
   * 查询某个用户加入了哪些群组
   * @param groupMapWhereInput
   * @returns GroupMap[]
   */
   async queryGroupMapList(
    groupMapWhereInput: Prisma.GroupMapWhereInput,
  ): Promise<GroupMap[]> {
   try {
    const groupList = await this.prisma.groupMap.findMany({
      where: groupMapWhereInput,
    });
    return groupList
   } catch (error) {}
  }

   /**
   * 查询某个用户的好友列表
   * @param userMapWhereInput
   * @returns UserMap[]
   */
   async queryUserMapList(
    userMapWhereInput: Prisma.UserMapWhereInput,
  ): Promise<UserMap[]> {
   try {
    const userList = await this.prisma.userMap.findMany({
      where: userMapWhereInput,
    });
    return userList
   } catch (error) {}
  }

  /**
   * 查询群组消息
   * @param groupMessage
   * @returns
   */
  async queryGroupMessage(
    groupMessage: Prisma.GroupMessageFindManyArgs,

  ): Promise<GroupMessage[]> {
    try {
      const groupRes = await this.prisma.groupMessage.findMany(groupMessage);
      return groupRes
     } catch (error) {}
  }

  /**
   * 查询私聊消息
   * @param friendMessage
   * @returns
   */
  async queryFriendMessage(
    friendMessage: Prisma.FriendMessageFindManyArgs,

  ): Promise<FriendMessage[]> {
    try {
      const friendMessageRes = await this.prisma.friendMessage.findMany(friendMessage);
      return friendMessageRes
     } catch (error) {}
  }

  /**
   * 根据用户ID查处用户名
   * @param userIds 用户ID组成的列表
   * @returns 查询到的用户名列表
   */
  async queryUsernameByUserIds(
    userIds: string[],
  ): Promise<string[]> {
    try {
      const usernameList = await this.prisma.user.findMany({
        where: {
          userId: {
            in: userIds
          }
        },
        select: {
          username: true
        }
      });
      return usernameList.map(item => item.username)
     } catch (error) {}
  }

  /**
   * 查询某个群组中的用户列表
   * @param groupId
   * @returns userArr
   */
  async getGroupUsers(groupId: string): Promise<User[]> {
    try {
      const userIdList = await this.prisma.groupMap.findMany({
        where: { groupId: groupId },
        select: { userId: true }
      })
      const userArr = []
      for (const user of userIdList) {
        const data = await this.prisma.user.findUnique({
          where: {
            userId: user.userId
          }
        })
        userArr.push(data)
      }
      return userArr
    } catch (error) {

    }
  }

  /**
   * 创建群组关联表数据
   * @param groupMap
   * @returns GroupMap
   */
  async createGroupMap(
    groupMap: Prisma.GroupMapCreateInput
  ): Promise<GroupMap> {
    try {
      const group = await this.prisma.groupMap.create({
        data: groupMap,
      });
      return group
     } catch (error) {}
  }

   /**
   * 创建群组关联表数据(批量入群)
   * @param groupMapList
   * @returns number
   */
   async createGroupMapMany(
    groupMapList: Prisma.GroupMapCreateManyInput[]
  ): Promise<number> {
    try {
      const groupCount = await this.prisma.groupMap.createMany({
        data: groupMapList,
      });
      return groupCount.count
     } catch (error) {}
  }

  /**
   * 创建群组数据
   * @param group
   * @returns Group
   */
  async createGroup(
    group: Prisma.GroupCreateInput
  ): Promise<Group> {
    try {
      const groupRes = await this.prisma.group.create({
        data: group,
      });
      return groupRes
     } catch (error) {}
  }

    /**
   * 创建朋友关联表数据
   * @param userMap
   * @returns UserMap
   */
    async createUserMap(
      userMap: Prisma.UserMapCreateInput
    ): Promise<UserMap> {
      try {
        const group = await this.prisma.userMap.create({
          data: userMap,
        });
        return group
       } catch (error) {}
    }

  /**
   * 添加群组消息记录
   * @param groupMessage
   * @returns
   */
  async createGroupMessage(
    groupMessage: Prisma.GroupMessageCreateInput
  ): Promise<GroupMessage> {
    try {
      const groupRes = await this.prisma.groupMessage.create({
        data: groupMessage,
      });
      return groupRes
     } catch (error) {}
  }


  /**
   * 添加私聊消息记录
   * @param friendMessage
   * @returns
   */
  async createUserMessage(
    friendMessage: Prisma.FriendMessageCreateInput
  ): Promise<FriendMessage> {
    try {
      const friendRes = await this.prisma.friendMessage.create({
        data: friendMessage,
      });
      return friendRes
     } catch (error) {}
  }

  /**
   * 删除群关系 -- 退群
   * @param groupMapInput
   */
  async removeGroupMap(
    groupMapInput: Prisma.GroupMapDeleteArgs
  ): Promise<GroupMap> {
    try {
      const groupMapRes = await this.prisma.groupMap.delete(groupMapInput)
      return groupMapRes
    } catch (error) {

    }
  }

   /**
   * 删除好友关系
   * @param userMapInput
   */
   async removeUserMap(
    userMapInput: Prisma.UserMapDeleteArgs
  ): Promise<UserMap> {
    try {
      const userMapRes = await this.prisma.userMap.delete(userMapInput)
      return userMapRes
    } catch (error) {

    }
  }
}
